package gunfire.cards;

import basemod.abstracts.CustomCard;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import gunfire.GunfireMod;
import gunfire.actions.ElementalEffectAction;
import gunfire.characters.Taizi;

import static gunfire.GunfireMod.makeCardPath;

public class ElementSword extends CustomCard {

    public static final String ID = GunfireMod.makeID(ElementSword.class.getSimpleName());
    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG = makeCardPath("ElementSword.png");
    private static final CardRarity RARITY = CardRarity.UNCOMMON;
    private static final CardType TYPE = CardType.ATTACK;
    private static final CardTarget TARGET = CardTarget.ENEMY;
    public static final CardColor COLOR = Taizi.Enums.COLOR_GRAY;
    private static final int COST = 2;
    private int elementType; //0火1毒2电

    public ElementSword() {
        super(ID, NAME, IMG, COST, DESCRIPTION, TYPE, COLOR, RARITY, TARGET);
        this.baseDamage = 10;
        this.baseMagicNumber = this.magicNumber = 3;
        this.selfRetain = true;
        this.elementType = -1;

        this.tags.add(GunfireMod.ELEMENT_DAMAGE_CARD);
        this.damageType = this.damageTypeForTurn = GunfireMod.ELEMENT_DAMAGE;
    }

    public ElementSword(int elementType){
        this();
        this.elementType = elementType;
        this.refreshDescAndImg();
    }

    @Override
    public void upgrade() {
        if (!this.upgraded) {
            this.upgradeName();
            this.upgradeDamage(5);
            this.upgradeMagicNumber(1);
        }
    }

    private void refreshDescAndImg(){
        if(this.elementType != -1){
            this.loadCardImage(makeCardPath("ElementSword" + this.elementType + ".png"));
            this.rawDescription = cardStrings.EXTENDED_DESCRIPTION[this.elementType];
            this.initializeDescription();
        }
    }

    @Override
    public void triggerWhenDrawn() {
        //抽到的时候随机指定一种元素
        this.elementType = AbstractDungeon.cardRandomRng.random(2);
        this.refreshDescAndImg();
    }

    @Override
    public void triggerWhenCopied() {
        //复制的时候随机指定一种元素
        if(this.elementType == -1){
            this.elementType = AbstractDungeon.cardRandomRng.random(2);
            this.refreshDescAndImg();
        }
    }

    @Override
    public AbstractCard makeStatEquivalentCopy() {
        //复制药水联动
        ElementSword c = (ElementSword) super.makeStatEquivalentCopy();
        c.elementType = this.elementType;
        c.refreshDescAndImg();
        return c;
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if(this.elementType == 0){
            this.addToBot(new DamageAction(m, new DamageInfo(p, damage, GunfireMod.ELEMENT_DAMAGE), AbstractGameAction.AttackEffect.FIRE));
            this.addToBot(new ElementalEffectAction(m, p, this.magicNumber, GunfireMod.ElementalEffect.Burning));
        }
        if(this.elementType == 1){
            this.addToBot(new DamageAction(m, new DamageInfo(p, damage, GunfireMod.ELEMENT_DAMAGE), AbstractGameAction.AttackEffect.POISON));
            this.addToBot(new ElementalEffectAction(m, p, this.magicNumber, GunfireMod.ElementalEffect.Decay));
        }
        if(this.elementType == 2){
            this.addToBot(new DamageAction(m, new DamageInfo(p, damage, GunfireMod.ELEMENT_DAMAGE), AbstractGameAction.AttackEffect.LIGHTNING));
            this.addToBot(new ElementalEffectAction(m, p, this.magicNumber, GunfireMod.ElementalEffect.Shock));
        }
    }

    @Override
    public void atTurnStart() {
        //回合开始时切换到下一种元素
        this.elementType = (this.elementType + 1) % 3;
        this.refreshDescAndImg();
    }

    @Override
    public AbstractCard makeCopy() {
        if(this.elementType == -1)
            return new ElementSword();
        return new ElementSword(this.elementType);
    }
}
